 /*
 * Column.java
 *
 * Created on May 4, 2006, 10:30 PM
 * 01, Apr 2011; In order to implement support for multiple SQL DB implementations and,
 *               this class was changed to abstrat. the intention is to implement a factory pattern for such suppoort.
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 *
 *       6, April, 2011. Relabeled as abstract.
 */

package portfolio.dbHost.dataBases.dbTable.tblColumn;

/**
 *
 * @author ACA
 */
    
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.*;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import portfolio.configMessage.configMessageEvent;
import portfolio.configMessage.configMessageFrame;
import portfolio.configMessage.configMessageListener;
import portfolio.dbHost.dataBases.dbTable.Table;
import portfolio.mainScreen;

public abstract class Column implements configMessageListener, MouseListener
{
    protected mainScreen         mainFrame  = null;
    protected Table              tableDB    = null;
    protected Connection         connection = null;
    
    private   configMessageFrame msgFrame   = null;
    
    private String     sField      = null;  //i.e. column name
/*0j0*/    private String     sType       = null;
/*0j0*/    private String     sNull       = null;
/*0j0*/    private String     sKey        = null;
//    private String     sDefault    = null;
//    private String     sExtra      = null;
//
    private JTree      dbaseTree    = null;

    public static final String COLUMN_DATE = "date";
    public static final String COLUMN_TIME = "time";

    private final String        ACTION_DB_TAB_DROP_COLUMN  = "Drop column";
    private final String        ACTION_NEW_PROJECT_INDEX   = "Index";
    private final String        ACTION_NEW_PROJECT_EXCLUDE = "Exclude";
    private final String        ACTION_NEW_PROJECT_DEP_VAR = "Dependent Var.";
    private static final String ACTION_NEW_PROJECT_STOCK   = "Stock Index";
    private static final String ACTION_NEW_PROJECT_R_FREE  = "Risk Free";
    private boolean bIndexFlag          = false;
    private boolean bExcludeFlag        = false;
    private boolean bTrainSetFlag       = false;
    private boolean bOptPrjStock        = false;
    private boolean bOptRiskFree        = false;
    /**
     *
     */
    public void okSelected(configMessageEvent e) 
    {
        try {
            String sCommand = msgFrame.getSQLStatementMnemonic();
            if        (sCommand.equals(this.ACTION_DB_TAB_DROP_COLUMN)) {
                String sTable = tableDB.getTableName();
                
                Column.drop (connection, sTable, sField);
                mainFrame.displayMessage("Column.okSelected 69", "Drop column " + sField + " successful.");
            }
            tableDB.getDBase().getDBHost().buildDBTree();       
//            mainFrame.updateDBTreePanel();
            tableDB.getDBase().getDBHost().updateDBTree(); //mainFrame.getDBasePanel());
            msgFrame.dispose();
        } catch (Exception ex) {
            mainFrame.displayMessage("Column.okSelected 75", ex.toString());
        }
    }
    /**
     *
     */
    public void cancelSelected(configMessageEvent e) 
    {
        msgFrame.dispose();
    }
    /**
     *
     */

    public void mouseClicked(MouseEvent arg0) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent arg0) {
//        throw new UnsupportedOperationException("Not supported yet.");
        Object o = arg0.getSource();
        JMenuItem jTempMenuItem = (JMenuItem) o;

        try {
            String sCommand = jTempMenuItem.getActionCommand();
            if        (sCommand.equals(this.ACTION_DB_TAB_DROP_COLUMN)) {
                msgFrame = new configMessageFrame (mainFrame, this.sField, this.ACTION_DB_TAB_DROP_COLUMN, 250, 120);
                msgFrame.showMessageFrame();
                msgFrame.addConfigMessageListener(this);
            } else if (sCommand.equals(this.ACTION_NEW_PROJECT_INDEX)) {
                bIndexFlag = true;
            } else if (sCommand.equals(this.ACTION_NEW_PROJECT_EXCLUDE)) {
                bExcludeFlag = true;
            } else if (sCommand.equals(this.ACTION_NEW_PROJECT_DEP_VAR)) {
                bTrainSetFlag = true;
            } else if (sCommand.equals (Column.ACTION_NEW_PROJECT_STOCK)) {
//ACTION_NEW_PROJECT_STOCK
                bOptPrjStock = true;
            } else if (sCommand.equals(Column.ACTION_NEW_PROJECT_R_FREE)) {
//ACTION_NEW_PROJECT_R_FREE
                bOptRiskFree = true;
            }
        } catch (Exception ex) {
            mainFrame.displayMessage("Column.mousePressed 112", ex.getMessage());
        }
    }

    public void mouseReleased(MouseEvent arg0) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent arg0) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent arg0) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }
    /**
     * Creates a new instance of Column
     */
    public Column (Table tableDBParam) throws Exception
    {
        tableDB = tableDBParam;
        if (tableDB == null)
            throw new Exception ("null tableDB parameter.");
        mainFrame  = tableDB.getDBase().getDBHost().getMainScreen();
        connection = tableDB.getDBase().getDBHost().getConnection();

        if (mainFrame == null)
            throw new Exception ("null mainFrame parameter.");

    } /* end constructor Column */
    /**
     *
     */
    public String getField ()
    {
        return sField;
    }
    /**
     *
     */
    public void setField (String sFieldParam)
    {
        sField = sFieldParam;
    }
    /**
     *
     */
/*0j0*/    public String getType ()
/*0j0*/    {
/*0j0*/        return sType;
/*0j0*/    }
//    /**
//     *
//     */
/*0j0*/    public void setType (String sTypeParam)
/*0j0*/    {
/*0j0*/        sType = sTypeParam;
/*0j0*/    }
//    /**
//     *
//     */
/*0j0*/    public String getNull ()
/*0j0*/    {
/*0j0*/        return sNull;
/*0j0*/    }
//    /**
//     *
//     */
/*0j0*/    public void setNull (String sNullParam)
/*0j0*/    {
/*0j0*/        sNull= sNullParam;
/*0j0*/    }
//    /**
//     *
//     */
/*0j0*/    public String getKey ()
/*0j0*/    {
/*0j0*/        return sKey;
/*0j0*/    }
    /**
     *
     */
    public void setKey (String sKeyParam)
    {
        sKey = sKeyParam;
    }
//    /**
//     *
//     */
//    public String getDefault ()
//    {
//        return sDefault;
//    }
//    /**
//     *
//     */
//    public void setDefault (String sDefaultParam)
//    {
//        sDefault = sDefaultParam;
//    }
//    /**
//     *
//     */
//    public String getExtra ()
//    {
//        return sExtra;
//    }
//    /**
//     *
//     */
//    public void setExtra (String sExtraParam)
//    {
//        sExtra = sExtraParam;
//    }
    /**
     *
     */
//    abstract public DefaultMutableTreeNode processTreeHierarchy ();
    public DefaultMutableTreeNode processTreeHierarchy ()
    {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode (sField);

        node.add (new DefaultMutableTreeNode (sType));
        node.add (new DefaultMutableTreeNode (sNull));
        return (node);
    } /* end method processHierarchy */
    /**
     *
     */
    public void getPopupOptions (JTree dbTreeParam, int iXPos, int iYPos)
    {
        dbaseTree = dbTreeParam;       

        JPopupMenu treeOptionsMenu = new JPopupMenu ("Options");
        dbaseTree.add(treeOptionsMenu);

        JMenuItem jItem = new JMenuItem (this.ACTION_DB_TAB_DROP_COLUMN);
        jItem.addMouseListener (this);
        treeOptionsMenu.add (jItem); 
        
        treeOptionsMenu.setLocation (iXPos, iYPos);
        treeOptionsMenu.show (treeOptionsMenu, iXPos, iYPos);
        
        return;
    } /* end method getPopupOptions */    
    /**
     *
     */
    public void getProjectPopupOptions (JTree dbTreeParam, int iXPos, int iYPos)
    {
        if (dbTreeParam == null) {
            mainFrame.displayMessage("Column.getProjectPopupOptions 289", "null tree.");
            return;
        }

        JPopupMenu treeOptionsMenu = new JPopupMenu ("Options");
        dbTreeParam.add(treeOptionsMenu);

        JMenuItem jIndexItem = new JMenuItem (this.ACTION_NEW_PROJECT_INDEX);
        jIndexItem.addMouseListener (this);
        treeOptionsMenu.add (jIndexItem); 

        JMenuItem jExcludeItem = new JMenuItem (this.ACTION_NEW_PROJECT_EXCLUDE);
        jExcludeItem.addMouseListener(this);
        treeOptionsMenu.add (jExcludeItem); 

        JMenuItem jVariableItem = new JMenuItem (this.ACTION_NEW_PROJECT_DEP_VAR);
        jVariableItem.addMouseListener(this);
        treeOptionsMenu.add (jVariableItem); 
//ACTION_NEW_PROJECT_STOCK
        JMenuItem jStockItem = new JMenuItem (Column.ACTION_NEW_PROJECT_STOCK);
        jStockItem.addMouseListener(this);
        treeOptionsMenu.add (jStockItem); 
//ACTION_NEW_PROJECT_R_FREE
        JMenuItem jRFreeItem = new JMenuItem (Column.ACTION_NEW_PROJECT_R_FREE);
        jRFreeItem.addMouseListener(this);
        treeOptionsMenu.add (jRFreeItem); 
        
        treeOptionsMenu.setLocation (iXPos, iYPos);
        treeOptionsMenu.show (treeOptionsMenu, iXPos, iYPos);

        return;
    } /* end method getPopupOptions */    
    /**
     *
     */
    public Object getTreeObject (Object [] oPathParam)
    {
        if (oPathParam == null) {
            //error, no path param
        }
            //Vector vOut = new Vector ();
            //vOut.add("<Column>");
            return this;        
    } /* end method getTreeObject */
    /**
     * 
     */
    public static void create (Connection connectParam, String sTable, String sColumnParam) throws SQLException
    {
        Statement statement = null;
        statement = connectParam.createStatement ();
        ResultSet rs;
        
        try {
            //statement.execute ("USE portfolio;");
            rs = statement.executeQuery ("SELECT * FROM " + sTable + ";");
            rs.findColumn (sColumnParam);
        } catch ( SQLException sqlex ) {
                statement.execute ("ALTER TABLE " + sTable + " ADD COLUMN " + sColumnParam + " ;");
        }        
        return;
    }    
    static public void drop (Connection connectionParam, String sTableParam, String sColumnParam) throws Exception
    {
        if (connectionParam == null)
            throw new Exception ("No connection provided.");
        if (sTableParam == null)
            throw new Exception ("No table name provided.");
        if (sColumnParam == null)
            throw new Exception ("No Column name provided.");
        
        Statement statement = null;
        
        try {
            //statement.execute ("USE portfolio;");
            //rs = statement.executeQuery ("SELECT * FROM " + sTable + ";"); //returnSeries;");
            //rs.findColumn (sColumnParam);
            statement = connectionParam.createStatement();
            statement.execute ("ALTER TABLE " + sTableParam + " DROP COLUMN " + sColumnParam + ";");
        } catch ( SQLException sqlex ) {
            throw new Exception (sqlex);
        }        
        return;
    }
    /**
     *
     */
    public boolean getIndexFlag ()
    {
        return bIndexFlag;
    }
    public boolean getExcludeFlag ()
    {
        return bExcludeFlag;
    }
    public boolean getTrainSetFlag ()
    {
        return bTrainSetFlag;
    }
    public boolean getOptPrjStock ()
    {
        return bOptPrjStock;
    }
    public boolean getOptRiskFree ()
    {
        return bOptRiskFree;
    }
    /**
     *
     */
    public void setIndexFlag (boolean bFlagParam)
    {
        bIndexFlag          = bFlagParam;
    }
    public void setExcludeFlag (boolean bFlagParam)
    {
        bExcludeFlag        = bFlagParam;
    }
    public void setTrainSetFlag (boolean bFlagParam)
    {
        bTrainSetFlag         = bFlagParam;
    }
    public void setOptPrjStock (boolean bOptPrjStockParam)
    {
        bOptPrjStock = bOptPrjStockParam;
    }
    public void setOptRiskFree (boolean bOptRiskFreeParam)
    {
        bOptRiskFree = bOptRiskFreeParam;
    }
    /**
     *
     */
    public Table getDBTable ()
    {
        return tableDB;
    }
}/* end class Column */